function main()
clear all;
close all;
%%%%%%%%%%%%%%%%%%%%%%%
% MEMC main matlab task
%%%%%%%%%%%%%%%%%%%%%%%

% get file name
[fileName, pathName, filterIndex] = uigetfile(                       ...
       {'*.avi;*.mp4;*.mpg;*.div; *.divx',                           ...
        'all video stream files(*.avi, *.mp4, *.mpg, *.div, *.divx)';...
        '*.*',  'All Files (*.*)'}, 'Pick a file');
streamFile    = [pathName, fileName];
% init basic parameters
streamStart   = 5;
inRate        = 24;
outFrames     = 10;
outRate       = 60;
initPhase     = 0.25;
pullPtn       = [1 1];
numPtn        = length(pullPtn);
resultPath    = './result';
blkSize       = [8 8];     %follow 3DrsMe.prm
regionSz      = [240 135]; %follow 3DrsMe.prm
lrRatio       = [2 2];     %follow 3DrsMe.prm
prmFileName   = './3DrsMe.prm';
% create wait bar
hWaitBar      = waitbar(0, 'in processing, 0% done, please wait ...');
% create output video

outTime       = (1/inRate)*initPhase; % initial output timing
outIndex      = 1;
inFrame       = get_frame(streamFile, pullPtn, streamStart);
inFrame       = get_frame;


% init mex function call parameters
yuvCur        = permute(rgb2ycbcr(inFrame.cdata), [2 1 3]); %[w * h * 3]
yuvPre        = yuvCur;
picSize       = size(yuvCur);
blkNum        = picSize(1:2) ./blkSize;
blkNumLr      = ceil(blkNum./lrRatio);
regionNum     = ceil(picSize(1:2) ./ regionSz);
flagNew       = 0;

bkinfoPre     = struct('vx'  , int16(zeros(blkNum)), ...
                       'vy'  , int16(zeros(blkNum)), ...
                       'sad' , int16(zeros(blkNum)), ...
                       'dmat', int16(zeros(blkNum)), ...
                       'type', int8 (zeros(blkNum)));
bkinfoLr      = struct('vx'  , int16(zeros(blkNumLr)), ...
                       'vy'  , int16(zeros(blkNumLr)), ...
                       'sad' , int16(zeros(blkNumLr)), ...
                       'dmat', int16(zeros(blkNumLr)), ...
                       'type', int8 (zeros(blkNumLr)));
                   
rsltLr        = struct('bv'  , bkinfoLr, ...
                       'sv'  , bkinfoLr, ...
                       'ds'  , bkinfoLr, ...
                       'zv'  , bkinfoLr  );
                                      
rsltPre       = struct('bv'  , bkinfoPre, ...
                       'sv'  , bkinfoPre, ...
                       'ds'  , bkinfoPre, ...
                       'zv'  , bkinfoPre  );
rsltGme       = struct('vx'    , int16(zeros(regionNum)), ...
                       'vy'    , int16(zeros(regionNum)), ...
                       'period', uint8(zeros(regionNum)));

vfmtPre       = video_format('VDF_PROG');
ifmtPre       = image_format('IMF_YUV444_PLAN');
vfmtCur       = vfmtPre;
ifmtCur       = ifmtPre;

% main processing loop
while (outIndex < outFrames)
    waitbar(outIndex/outFrames, hWaitBar, ...
        [ 'in processing,', num2str(outIndex/outFrames*100, '%2.2f'),'% done, please wait ...']);
    while(outTime >= 1/inRate)
        outTime = outTime - 1/inRate;
        while ~flagNew
            [inFrame, flagNew] = get_frame;
        end
        yuvPre    = yuvCur;
        yuvCur    = permute(rgb2ycbcr(inFrame.cdata), [2 1 3]); %[w * h * 3]
    end
    rsltCur  = VPE_MEX ( yuvPre, vfmtPre, ifmtPre, yuvCur, vfmtCur, ifmtCur, rsltGme, rsltLr, rsltPre, prmFileName);
    rsltPre  = rsltCur;
    outTime  = outTime + 1/outRate;
    outIndex = outIndex+1; 
end
close(hWaitBar);





function ifmt = image_format(strFmt)
ifmt         = uint32(0);
switch strFmt
    case 'IMF_RGB444_PLAN'
        ifmt = bitset(bitset(bitset(ifmt, 32, 1),28,1), 26, 1);
    case 'IMF_YUV444_PLAN'
        ifmt = bitset(bitset(bitset(ifmt, 32, 1),27,1), 26, 1);
    case 'IMF_YUV422_PLAN'
        ifmt = bitset(bitset(bitset(ifmt, 31, 1),27,1), 26, 1);
    case 'IMF_YUV420_PLAN'
        ifmt = bitset(bitset(bitset(ifmt, 30, 1),27,1), 26, 1);
    case 'IMF_YUV411_PLAN'
        ifmt = bitset(bitset(bitset(ifmt, 29, 1),27,1), 26, 1);
    case 'IMF_RGB444_PACK'
        ifmt = bitset(bitset(bitset(ifmt, 32, 1),28,1), 25, 1);
    case 'IMF_YUV444_PACK'
        ifmt = bitset(bitset(bitset(ifmt, 32, 1),27,1), 25, 1);
    otherwise
        display(['unsupported image format : ', strFmt]);
end
%return ifmt;



function vfmt = video_format(strFmt)
vfmt  = uint32(0);
switch strFmt
    case 'VDF_PROG',
        vfmt  = uint32(1);
    case 'VDF_TFFRM'
        vfmt  = uint32(2);
    case 'VDF_BFFRM'
        vfmt  = uint32(4);
    case 'VDF_TFLD'
        vfmt  = uint32(8);
    case 'VDF_BFLD'
        vfmt  = uint32(16);
    otherwise
        display(['unsupported video format', strFmt]);
end
%return vfmt;

